package net.sivashov.grailssample.ds

import javax.sql.DataSource
import java.sql.Connection
import java.sql.SQLException
import java.util.logging.Logger

/**
 * @author LSivashov
 * @since 2/25/13
 */
class CustomDataSource implements DataSource {

    def DataSource defaultDataSource

    Connection getConnection() throws SQLException {
        resolveDataSource().connection
    }

    Connection getConnection(String username, String password) throws SQLException {
        resolveDataSource().getConnection(username, password)
    }

    PrintWriter getLogWriter() throws SQLException {
        resolveDataSource().logWriter
    }

    void setLogWriter(PrintWriter out) throws SQLException {
        resolveDataSource().logWriter = out
    }

    void setLoginTimeout(int seconds) throws SQLException {
        resolveDataSource().loginTimeout = seconds
    }

    int getLoginTimeout() throws SQLException {
        resolveDataSource().loginTimeout
    }

    def <T> T unwrap(Class<T> iface) throws SQLException {
        resolveDataSource().unwrap(iface)
    }

    boolean isWrapperFor(Class<?> iface) throws SQLException {
        resolveDataSource().isWrapperFor(iface)
    }

    Logger getParentLogger() {
        resolveDataSource().parentLogger
    }

    def DataSource resolveDataSource() {
        DataSourceResolver.dataSource ?: defaultDataSource
    }
}
